package com.tsd.system.service.impl;

import com.tsd.CommonMsgNameConstants;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SysGroupPersonMapper;
import com.tsd.system.entity.SysGroupPerson;
import com.tsd.system.entity.SysGroupPersonExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysGroupPersonService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysGroupPerson 服务实现层
 *
 * @author zsy
 * @date 2020/12/29 15:52:20
 */
@Service
public class SysGroupPersonServiceImpl extends BaseServiceImpl implements SysGroupPersonService {

    @Resource
    private SysGroupPersonMapper sysGroupPersonMapper;

    @Override
    public SysGroupPerson getSysGroupPersonById(Long id) throws Exception {
        return sysGroupPersonMapper.selectByPrimaryKey(id);
    }

    @Override
    public SysGroupPersonExt queryBySid(String sid) throws Exception {
        return sysGroupPersonMapper.selectBySid(sid);
    }

    @Override
    public void deleteSysGroupPerson(List<String> sids, SysUser opUser) throws Exception {
        for (String sid : sids) {
            SysGroupPerson old = sysGroupPersonMapper.selectBySid(sid);
            super.checkEmpty(old, CommonMsgNameConstants.ERROR_MSG_RECORD_NOT_EXISTS);
            super.autoInjectBaseData(old, opUser, this.TYPE_DELETE);
            sysGroupPersonMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveSysGroupPerson(SysGroupPerson record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            sysGroupPersonMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            sysGroupPersonMapper.updateByPrimaryKeySelective(record);
        }
        // 校验部门下编码不能重复
        if (!HlpUtils.isEmpty(record.getGroup_sid()) && !HlpUtils.isEmpty(record.getCode())) {
            int codeNum = sysGroupPersonMapper.countByGroupSidAndCode(record.getGroup_sid(), record.getCode());
            if (codeNum > 1) {
                throw new HlpException("存在相同的员工编号，请重新输入");
            }
        }
        // 校验绑定人员不能绑定多个员工
        int userNum = sysGroupPersonMapper.countByUserSid(record.getUser_sid());
        if (userNum > 1) {
            throw new HlpException("用户名已被其他员工使用，请重新选择");
        }

    }

    @Override
    public List<SysGroupPersonExt> findSysGroupPersons(Map<String, Object> params, PageBean page) throws Exception {
        return sysGroupPersonMapper.selectByPage(params, page);
    }

    @Override
    public List<SysGroupPersonExt> findSysGroupPersons(Map<String, Object> params) throws Exception {
        return sysGroupPersonMapper.selectByParams(params);
    }

    @Override
    public void batchDeleteSysGroupPerson(List<String> sidList, SysUser opUser) throws Exception {
        deleteSysGroupPerson(sidList, opUser);
    }
}
